<?php
header("Content-Type:text/html; charset=utf-8");

require_once("../../connectMysql/connetMysql.class.php");
$db=new connetMysql;
$con=$db->getConnect();
mysqli_query($con,"SET names utf8");
session_start();               //为了将验证码打乱
$_SESSION['checkCode']=md5(time());     //将验证码的值存入session中
date_default_timezone_set('prc');
	require_once "email.class.php";
	//******************** 配置信息 ********************************
	$usermail = $_POST['usermail'];//发送给谁


	//$result = mysqli_query("SELECT * FROM user where usermail='".$usermail."'");

			$sql="SELECT * FROM user where usermail='".$usermail."'";
			$result = mysqli_query($con,$sql);
	if($row = mysqli_fetch_array($result))
  	{
		$username = $row["username"];
		$userpassword = $row["userpassword"];
		$regtime = time(); 
		$useractivecode = md5($username.$userpassword.$regtime);
		mysqli_query($con,"UPDATE user SET useractivecode = '".$useractivecode."' where usermail='".$usermail."'");	
	}
	else
	{
		$username = $_POST["username"];
		$userpassword = $_POST["userpassword"];
		$regtime = time(); 
		$useractivecode = md5($username.$userpassword.$regtime);
		mysqli_query($con,"INSERT INTO user (usermail,username,userpassword,useractivecode) VALUES ('$usermail', '$username', '$userpassword','$useractivecode')");	
	}	
	
	
	$smtpserver = "smtp.126.com";//SMTP服务器
	$smtpserverport =25;//SMTP服务器端口
	$smtpusermail = "pan_baowen@126.com";//SMTP服务器的用户邮箱
	
	$smtpuser = "pan_baowen";//SMTP服务器的用户帐号
	$smtppass = "13878429608";//SMTP服务器的用户密码
	$mailtitle = "用户账号激活";//邮件主题
	$mailcontent = "亲爱的".$username."：<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/> 
    <a href='http://localhost/registertest/register/active.php?verify=".$useractivecode."' target= 
'_blank'>http://localhost/registertest/register/active.php?verify=".$useractivecode."</a><br/> 
    如果以上链接无法点击，请将它复制到你的浏览器地址栏中进入访问，该链接24小时内有效。";//邮件内容
	$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
	//************************ 配置信息 ****************************
	$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
	$smtp->debug = false;//是否显示发送的调试信息
	$state = $smtp->sendmail($usermail, $smtpusermail, $mailtitle, $mailcontent, $mailtype);

	echo "<div style='width:300px; margin:36px auto;'>";
	if($state==""){
		echo "对不起，注册失败！请重新注册。";
		echo "<a href='register.html'>点此返回</a>";
		exit();
	}
	echo "注册成功！请登陆邮箱进行激活。";
	echo "<a href='register.html'>返回</a>";
	echo "</div>";
	mysqli_close($con);
?>